﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;

/// <summary>
/// Summary description for XL_KH
/// </summary>
public class XL_DH
{
    protected DataRow Dong;
    protected static String Tenbang = "DON_HANG";

    public object MaCTDH
    {
        get
        {
            return this.Dong["MCT"];
        }
        set
        {
           
        }
    }

    public int MaDH
    {
        get
        {
            return (int)this.Dong["MDH"];
        }
        set
        {
            this.Dong["MDH"] = value;
        }
    }

    public int MTK
    {
        get
        {
            return (int)this.Dong["MTK"];
        }
        set
        {
            this.Dong["MTK"] = value;
        }
    }

    public String TenTK
    {
        get
        {
            return this.Dong["Ten_TK"].ToString();
        }
        set
        {
            this.Dong["Ten_TK"] = value;
        }
    }

  
    public DateTime Ngaydat
    {
        get
        {
            return (DateTime)this.Dong["Ngaydat"];
        }
        set
        {
            this.Dong["Ngaydat"] = value;
        }
    }
    
    public Boolean TinhtrangTT
    {
        get
        {
            return (Boolean)this.Dong["Tinhtrang"];
        }
        set
        {
            this.Dong["Tinhtrang"] = value;
        }
    }
	
     public XL_DH()
    {
        String sql="select * from " + Tenbang;
        DataTable Bang  = DataProvider.ExecuteStructureQuery(sql);
        Dong = Bang.NewRow();
        Bang.Rows.Add(Dong);
    }

    public XL_DH(DataRow DongN)
	{
		//
		// TODO: Add constructor logic here
		//
        this.Dong = DongN;
	}

    public static ArrayList Doc_danh_sach()
    {
        ArrayList Kq = new ArrayList();
        string sql = "Select * ";
        sql += " From DON_HANG dh,TAI_KHOAN tk ";
        sql += " Where dh.mtk = tk.mtk";
        DataTable Bang = DataProvider.ExecuteQuery(sql);
        foreach (DataRow DT in Bang.Rows)
        {
            XL_DH LTK = new XL_DH(DT);
            Kq.Add(LTK);
        }
        return Kq;

    }
    /*
    public static ArrayList TimDH_theo_TenTK(String TenTK)
    {
        
        XL_TK TAI_KHOAN =  XL_TK.TimKH_theo_TenTK(TenTK);
        ArrayList DSDH = new ArrayList();

        String sql = "Select dh.MDH,dh.MTK,dh.Ngaydat,dh.Tinhtrang,tk.Ten_TK,ctdh.MCT ";
        sql += "From (TAI_KHOAN tk left join DON_HANG dh on dh.MTK = tk.MTK) left join CHI_TIET_DON_HANG ctdh on ctdh.MDH=dh.MDH ";
        sql += "Where dh.MTK = " + TAI_KHOAN.MTK;

        DataTable Bang = DataProvider.ExecuteQuery(sql);
        
            foreach (DataRow DT in Bang.Rows)
            {
                DSDH.Add(new XL_DH(DT));
            }
        
        return DSDH;
        

    }



    public static ArrayList TimDH_theo_Hoten(String Hoten)
    {

        ArrayList DSTK = XL_TK.TimKH_theo_Hoten(Hoten);
        String chuoi_MTK="";
        foreach (XL_TK DT in DSTK)
        {
            chuoi_MTK += DT.MTK + ","; 
        }
        chuoi_MTK = chuoi_MTK.Substring(0,chuoi_MTK.Length-2);
        
        ArrayList DSDH = new ArrayList();

        String sql = "Select dh.MDH,dh.MTK,dh.Ngaydat,dh.Tinhtrang,tk.Ten_TK,ctdh.MCT ";
        sql += "From (TAI_KHOAN tk left join DON_HANG dh on dh.MTK = tk.MTK) left join CHI_TIET_DON_HANG ctdh on ctdh.MDH=dh.MDH ";
        sql += "Where dh.MTK in (" + chuoi_MTK + ")";

        DataTable Bang = DataProvider.ExecuteQuery(sql);

        foreach (DataRow DT in Bang.Rows)
        {
            DSDH.Add(new XL_DH(DT));
        }

        return DSDH;

    }

    public static ArrayList TimDH_theo_MDH(int MDH)
    {
        ArrayList DSDH = new ArrayList();
        String sql = "Select dh.MDH,dh.MTK,dh.Ngaydat,dh.Tinhtrang,tk.Ten_TK,ctdh.MCT ";
        sql += "From (TAI_KHOAN tk right join DON_HANG dh on dh.MTK = tk.MTK) left join CHI_TIET_DON_HANG ctdh on ctdh.MDH=dh.MDH ";
        sql += "Where dh.MDH like '*" + MDH +"*'";

        DataTable Bang = DataProvider.ExecuteQuery(sql);

        foreach (DataRow DT in Bang.Rows)
        {
            DSDH.Add(new XL_DH(DT));
        }

        return DSDH;


    }

    public static XL_DH Tim1DH_theo_MDH(int MDH)
    {
        
        String sql = "Select dh.MDH,dh.MTK,dh.Ngaydat,dh.Tinhtrang,tk.Ten_TK,ctdh.MCT ";
        sql += "From (TAI_KHOAN tk right join DON_HANG dh on dh.MTK = tk.MTK) left join CHI_TIET_DON_HANG ctdh on ctdh.MDH=dh.MDH ";
        sql += "Where dh.MDH like '*" + MDH + "*'";

        DataTable Bang = DataProvider.ExecuteQuery(sql);
        if (Bang.Rows.Count > 0)
        {
            return new XL_DH(Bang.Rows[0]);
        }

        return null;


    }
    */

  

    public void Ghi() // Ghi tung dong
    {
        String sql = "select * from " + Tenbang;
        sql += " Where MTK = " + this.MTK;
        DataTable BDH = DataProvider.ExecuteQuery(sql);
        DataRow DH = BDH.Rows[0];
       
        DH["MDH"] = this.MaDH;
        DH["Ngaydat"] = this.Ngaydat;
        DH["Tinhtrang"] = this.TinhtrangTT;

        //Ghi Bang
        
        DataProvider.Save(DH, Tenbang);

    }
    public void Xoa()
    {

        String sql = "select * from " + Tenbang;
        sql += " Where MTK = " + this.MTK;
        DataTable BDH = DataProvider.ExecuteQuery(sql);
        DataRow DH = BDH.Rows[0];
       
        DataProvider.Delete(DH, Tenbang);

    }

}

  